package 二叉树相关问题.中度题;

public class 从叶节点开始的最小字符串 {
    String ans="~";  //因为~的数值比Z大
    public String smallestFromLeaf(TreeNode root) {
         dfs(root,new StringBuilder());
         return ans;
    }

    private void dfs(TreeNode root, StringBuilder builder) {
        if (root==null)
            return;
        builder.append((char)('a'+root.val)); //将数值转换为对应的字母
        if (root.left==null && root.right==null){
            builder.reverse();
            String S = builder.toString();
            builder.reverse();
            if (S.compareTo(ans)<0)
                ans=S;
        }
        dfs(root.left,builder);
        dfs(root.right,builder);
        builder.deleteCharAt(builder.length()-1);
    }
}
